<?php
/**
 * Created by PhpStorm.
 * User: andyge
 * Date: 15/11/8
 * Time: 下午7:46
 */

class DBRes
{
    var $file;
    #var $md5;
    var $tag;
    var $time;      // upload time
}

class DB
{
    var $file;

    function DB($f) {
        $this->file = $f;
    }

    public function init() {
        $db = sqlite_open($this->file);
        if(!$db) {
            return sqlite_last_error($db);
        }

        $sql =<<<EOF
      CREATE TABLE RES
      (ID INT PRIMARY KEY     NOT NULL AUTOINCREMENT,
      FILE           TEXT    NOT NULL,
      TAG            TEXT,
      TIME           TEXT    NOT NULL);
EOF;
        $ret = sqlite_exec($this->db, $sql);
        if(!$ret) {
            $err = sqlite_last_error($db);
            sqlite_close($db);
            return $err;
        }

        sqlite_close($db);
        return 0;
    }

    public function addRes($res) {
        $db = sqlite_open($this->file);
        if(!$db) {
            return sqlite_last_error($db);
        }
        $sql = "INSERT INTO RES (FILE, TAG, TIME) VALUES ($res->file, $res->tag, $res->time);";
        $ret = sqlite_exec($db, $sql);
        if(!$ret) {
            $err = sqlite_last_error($db);
            sqlite_close($db);
            return $err;
        }
        return 0;
    }

    public function delRes($res) {

    }

    public function getRes($num) {
        $db = sqlite_open($this->file);
        if(!$db) {
            return sqlite_last_error($db);
        }
        $sql = "SELECT * FROM RES ORDER BY $ID";
        return 0;
    }
}
?>